import type { NextApiRequest, NextApiResponse } from 'next';

let users = [
  { id: 1, name: 'John Doe' },
  { id: 2, name: 'Jane Doe' },
  { id: 3, name: 'Bob Smith' },
];

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method === 'GET') {
    res.status(200).json(users);
  } else if (req.method === 'POST') {
    const { name } = req.body;
    const newUser = { id: Date.now(), name };
    console.log(name);
    users.push(newUser);
    const message = `User ${name} created successfully`;
    res.status(201).json(message);
  } else {
    res.status(405).json({ error: 'Method Not Allowed' });
  }
}
